//
//  MoreViewController.m
//  Tiger
//
//  Created by King on 11-9-9.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MoreViewController.h"
#import "ContactViewController.h"
#import "ProductListViewController.h"
#import "SubscribeViewController.h"
#import "CollectionViewController.h"
#import "PCollectionViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "ActListViewController.h"
#define kCustomRowCount     4

@implementation MoreViewController
@synthesize listData;
@synthesize parsedDataArray;



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    p_MoreTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 390) style:UITableViewStylePlain];
    p_MoreTableView.delegate =self;
    p_MoreTableView.dataSource = self;
    
    
    p_LessGray = [UIColor colorWithRed:245/255.f green:245/255.f blue:245/255.f alpha:1.0f];
    [p_LessGray retain];
    [p_MoreTableView setBackgroundColor:p_LessGray];
    [super viewDidLoad];
//	self.title = @"更多";
//	[self.navigationItem setTitleColor:[UIColor clearColor]];	
    if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"top.png"] forBarMetrics: UIBarMetricsDefault];
        self.navigationController.navigationBar.clipsToBounds = YES;
        
    }
	[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"top.png"]];
	self.navigationController.navigationBar.tintColor =  [UIColor colorWithRed:33.0/255 green:32.0/255 blue:32.0/255 alpha:1.0];
	

	
	
	
	NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
	
	NSString *plistPath = [rootPath stringByAppendingPathComponent:@"productData.plist"];
	
	if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
		
		plistPath = [[NSBundle mainBundle] pathForResource:@"productData" ofType:@"plist"];
		
	}
	
	NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
	
	self.parsedDataArray = [dict objectForKey:@"items"];
    [self.view addSubview:p_MoreTableView];
    

}

//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 70;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//	int count = [parsedDataArray count];
//    if (count == 0)
//	{
        return kCustomRowCount;
//    }
//    return count + 2;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
	//UIImageView* bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"m_cell_bg@2x.png"]];

	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:20];
        //添加箭头
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;	
        
        cell.backgroundView = nil;
        
        cell.backgroundColor = p_LessGray;
        cell.textLabel.backgroundColor =[UIColor clearColor];
        cell.contentView.backgroundColor =p_LessGray;
        cell.imageView.layer.shadowColor = [UIColor grayColor].CGColor;
        cell.imageView.layer.shadowOffset =CGSizeMake(3, 3);
        cell.imageView.layer.masksToBounds =YES;
        
        UIImageView* tAccView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrowin.png"]];
        tAccView.frame =CGRectMake(0, 0, 14, 14);
        cell.accessoryView =tAccView;
        [tAccView release];
        
        UIView* tCellBG =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
        [tCellBG setBackgroundColor:[UIColor redColor]];
        [cell setSelectedBackgroundView:tCellBG];
        [tCellBG release];
    }
    
	//[bgImageView release];
	// Set up the cell
	if(indexPath.row == 0)
	{
		cell.textLabel.text = @"联系我们";
		cell.imageView.image = [UIImage imageNamed:@"ta_1.png"];
	}
	if(indexPath.row == 1)
	{
		cell.textLabel.text = @"订阅";
		cell.imageView.image = [UIImage imageNamed:@"ta_2.jpg"];
	}
    if (indexPath.row == 2) {
        cell.textLabel.text = @"订购清单";
		cell.imageView.image = [UIImage imageNamed:@"ta_3.jpg"];
    }
    if (indexPath.row == 3) {
        cell.textLabel.text = @"优惠资讯";
		cell.imageView.image = [UIImage imageNamed:@"ta_4.png"];
    }
	
	
	
	if(indexPath.row >3){
		

	NSUInteger row = [indexPath row];
	NSDictionary *dict = [parsedDataArray objectAtIndex:row - 2];
	cell.textLabel.text = [dict objectForKey:@"name"];
	cell.imageView.image = [UIImage imageNamed:@"ta_2.jpg"];	
		
	}
	


	return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];	
	if (indexPath.row ==0) {
		ContactViewController *viewController = [[ContactViewController alloc] init];
		viewController.hidesBottomBarWhenPushed = YES;
		UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
									   initWithTitle:@"更多内容" 
									   style:UIBarButtonItemStylePlain 
									   target:self
									   action:nil];
		self.navigationItem.backBarButtonItem = backButton;
		//导航自带的动画
		[self.navigationController pushViewController:viewController animated:YES];
		[viewController release];
		
	} 
	
	else if (indexPath.row ==1) {
		SubscribeViewController *viewController = [[SubscribeViewController alloc] initWithNibName:@"SubscribeViewController" bundle:nil];
		viewController.hidesBottomBarWhenPushed = YES;
		UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
									   initWithTitle:@"更多内容" 
									   style:UIBarButtonItemStylePlain 
									   target:self
									   action:nil];
		self.navigationItem.backBarButtonItem = backButton;
		//导航自带的动画
		[self.navigationController pushViewController:viewController animated:YES];
		[viewController release];
		
	} 
    else if (indexPath.row == 2) {
//        SubscribeViewController *viewController = [[SubscribeViewController alloc] initWithNibName:@"SubscribeViewController" bundle:nil];
        PCollectionViewController *viewController = [[PCollectionViewController alloc] init];
		viewController.hidesBottomBarWhenPushed = YES;
		UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
									   initWithTitle:@"更多内容" 
									   style:UIBarButtonItemStylePlain 
									   target:self
									   action:nil];
		self.navigationItem.backBarButtonItem = backButton;
		//导航自带的动画
		[self.navigationController pushViewController:viewController animated:YES];
		[viewController release];
        [viewController reloadData];
    }
    else if (indexPath.row == 3) {
        
        ActListViewController *viewController = [[ActListViewController alloc] init];
		viewController.hidesBottomBarWhenPushed = YES;
		UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
									   initWithTitle:@"更多内容" 
									   style:UIBarButtonItemStylePlain 
									   target:self
									   action:nil];
		self.navigationItem.backBarButtonItem = backButton;
		//导航自带的动画
		[self.navigationController pushViewController:viewController animated:YES];
		[viewController release];
//        if (!m_popView.superview) {
//            [self.view addSubview:m_popView];
//        }
    }
	else if ([self.parsedDataArray count] > 0 && indexPath.row > 3) {
		ProductListViewController *viewController = [[ProductListViewController alloc] initWithNibName:@"ProductListViewController" bundle:nil];
		viewController.hidesBottomBarWhenPushed = YES;
		
		NSUInteger row = [indexPath row];
		NSDictionary *dict = [parsedDataArray objectAtIndex:row - 2];
		NSString *st_CateID = [dict objectForKey:@"cateID"];
		viewController.cateID = [st_CateID intValue];
		
		UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
									   initWithTitle:@"更多内容" 
									   style:UIBarButtonItemStylePlain 
									   target:self
									   action:nil];
		self.navigationItem.backBarButtonItem = backButton;
		
		//导航自带的动画
		[self.navigationController pushViewController:viewController animated:YES];
		[viewController release];

	}
	
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [listData release];
	[parsedDataArray release];
    [p_LessGray release];
    [p_MoreTableView release];
    [super dealloc];

}


@end
